<template>
	<view class="rootPage">
		<loading v-if='loading==-1' :status='loading' />
		<view v-else class="rootPage">
			<advert v-if="showFullScreenAd || showHomePopAd" @getAdvertMes="getAdvertMes"  class="advert" ref="advert" />	
			<fourIndex v-if="showPageIndex=='3'" :platformConfig="platformConfig" :pageIndexColor="pageIndexColor" ref="indexRefs" class=""  />
			<privacy-pop  @agreePrivacy="init"></privacy-pop>
		</view>
	</view>
</template>

<script>
	import advert from '@/components/advert/advert';
	import fourIndex from '@/components/fourIndex/fourIndex.vue'
	import utils from '../../utils/util.js'
	
	let app = getApp()
	export default {
		components: {
			advert,
			fourIndex
		},
		data() {
			return {
				showPageIndex: '3',
				pageIndexColor: '',
				colorDic: {
					'#FF2D4C': "red",
					'#FF6006': "orange",
					'#FFD204': "yellow",
					'#0ACED6': "green",
					'#4171FF': "blue"
				},
				platformConfig: {},
				pageId: '',//'1030505016894468096'
				showFullScreenAd: false,
				showHomePopAd: true,
				loading: 3,
				first: 0
			}
		},
		async onLoad(option) {
			// var info = uni.getSystemInfoSync()
			// console.log("获取微信版本库信息：", info.SDKVersion)
			//在main.js中定义promise对象，等app.vue初始化中请求完接口获取后resolve，保证app.vue onlunch执行完后再执行index页面onload方法
			await this.$onLaunched;
			this.$zhuge.track('进入首页')
			if (uni.getPrivacySetting) {
				uni.getPrivacySetting({
					success: res => {
						if (res.needAuthorization) {
							this.showPrivacy = true
							uni.hideTabBar()
						} else {
							this.init()
						}
					},
					fail: () => {},
					complete: () => {},
				})
			} else {
				console.log("不支持getPrivacySetting")
				this.init()
			}
		},
				
		methods: {
			async init() {
				// this.$checkLogin().then(res => {
				// 	this.loading = 3
				// 	this.getAdInfo()
				// 	this.getPlatformConfig()
				// }).catch(err => {
				// 	console.log(err);
				// 	this.loading = -1
				// })
				
				await this.$checkLogin()
				await this.getAdInfo() //放在这里调用,确保广告先加载
				await this.$refs.indexRefs.loadData() //调用模板组件初始化数据的方法
				this.loading = 3
			},
			
			/**
			 * 获取广告信息
			 */
			async getAdInfo() {
				// 查询广告
				let memberInfo = uni.getStorageSync('memberInfo')
				if (memberInfo != null) {
					await this.$refs.advert.getAdInfo();
				} else {
					this.showHomePopAd = false;
				}
			},
			getAdvertMes(showFullScreenAd, showHomePopAd) {
				console.log(showFullScreenAd, showHomePopAd);
				this.showFullScreenAd = showFullScreenAd;
				this.showHomePopAd = showHomePopAd;
			},
			/**
			 * 获取平台配置信息
			 */
			async getPlatformConfig() {
				let res = await app.globalData.getPlatformConfig();
				app.globalData.updateTabBarSkin();
				console.log(res)
				this.platformConfig = res;
				this.platformConfig['gradientColors1'] = utils.hexToRgba(this.platformConfig.gradientColors[0], 0.1);
				this.platformConfig['gradientColors2'] = utils.hexToRgba(this.platformConfig.gradientColors[1], 0.1);
				this.platformConfig['gradientColors3'] = utils.hexToRgba(this.platformConfig.gradientColors[0], 0.2);
				this.platformConfig['gradientColors4'] = utils.hexToRgba(this.platformConfig.gradientColors[1], 0.2);
				await app.globalData.getUserLocation();
				if(res.indexStyleType) {
					this.pageIndexColor = this.colorDic[res.baseColor];
					this.pageId = res.pageId;
					// this.showPageIndex = res.indexStyleType == -1 ? 1 : res.indexStyleType;
					//this.showPageIndex = '3'; //莱芜默认该模板
				} 
				// else {
				// 	this.showPageIndex = "1";
				// }
				app.globalData.hasIndexType = this.showPageIndex;
				return;
			},
		},
		
		onShow: function() {
			this.first++;
			console.log('首页onShow=============================',this.first)
			if(this.first > 1){
				this.$checkLogin()
			}
			uni.$on('loginUpdate', (data)=> {
				console.log('监听到session过期，重新加载数据',data)
				this.$refs.indexRefs.currentPlat = null;
				this.$refs.indexRefs.loadData()
				uni.$off('loginUpdate')
			})
			uni.$on('plantChange', (data)=> {
				console.log('监听到session过期，重新加载数据',data)
				this.$refs.indexRefs.currentPlat = null;
				this.$refs.indexRefs.loadData()
				uni.$off('plantChange')
			})
			
			
			// uni.$on('loginUpdate', (data)=> {
			// 	this.$checkLogin().then((res) => {
			// 		console.log('监听到session过期，重新加载数据',data)
			// 		this.$refs.indexRefs.currentPlat = null;
			// 		this.$refs.indexRefs.loadData()
			// 	})
			// 	uni.$off('loginUpdate')
			// })
		},
		
		
		/**
		 * 监听页面滚动 
		 */
		onPageScroll: function(e) {
			if(this.showPageIndex!="1") return;
			// if (e.scrollTop > 200) {
			// 	if(!this.$refs.indexRefs.isPageScroll) {
			// 		this.$refs.indexRefs.isPageScroll = true;
			// 	}
			// } else {
			// 	if(this.$refs.indexRefs.isPageScroll) {
			// 		this.$refs.indexRefs.isPageScroll = false;
			// 	}
			// }
		},
		/** 触底加载更多 */
		onReachBottom: function() {
			console.log('onReachBottom')
			// if(!this.showPageIndex=="1" && !this.showPageIndex=="3") return;//只有模版一和模版三监听页面下拉加载
			// if (this.$refs.indexRefs.haveMore) {
			// 	this.$refs.indexRefs.params.page++;
			// 	this.$refs.indexRefs.getMerListData();
			// }
		},
		/** 下拉刷新列表 */
		onPullDownRefresh: function() {
			// if(!this.showPageIndex=="1" && !this.showPageIndex=="3") return;//只有模版一和模版三监听页面下拉加载
			// this.$refs.indexRefs.params.page = 1;
			// this.$refs.indexRefs.params.haveMore = true;
			// this.$refs.indexRefs.params.merchantStoreList = [];
			// this.$refs.indexRefs.params.templete = [];
			// this.$refs.indexRefs.params.getMerListData();
			// setTimeout(() => {
			// 	uni.stopPullDownRefresh();
			// }, 2000);
		},
		/**
		 * 分享
		 */
		onShareAppMessage: function() {
			return {
				title: '民泰积分商城'
			};
		},
		
	}
</script>

<style scoped>
	.rootPage {
		position: relative;
		width: 100%;
		height: 100%;
	}
	.advert {
		height: 100vh;
		z-index: 999;
		width: 100vw;
		/* position: absolute; */
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>

